package com.xja.git.control;

import com.xja.git.entity.BlogUser;
import com.xja.git.service.BlogUserService;
import com.xja.git.service.CategoryService;
import com.xja.git.service.ConmmentServcie;
import com.xja.git.service.EntryService;
import com.xja.git.utils.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 作者：罗帅
 * 当前类所在包：com.xja.git.control
 * 创建时间：2022/8/17下午 5:15 星期三
 * 此类简介：
 */
@RestController
public class BlogUserController {
    @Autowired
    private BlogUserService blogUserService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ConmmentServcie conmmentServcie;
    @Autowired
    private EntryService    entryService;

    private Logger logger = LoggerFactory.getLogger(BlogUserController.class);

    public BlogUserService getBlogUserService() {
        return blogUserService;
    }

    public void setBlogUserService(BlogUserService blogUserService) {
        this.blogUserService = blogUserService;
    }

    public CategoryService getCategoryService() {
        return categoryService;
    }

    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    public ConmmentServcie getConmmentServcie() {
        return conmmentServcie;
    }

    public void setConmmentServcie(ConmmentServcie conmmentServcie) {
        this.conmmentServcie = conmmentServcie;
    }

    public EntryService getEntryService() {
        return entryService;
    }

    public void setEntryService(EntryService entryService) {
        this.entryService = entryService;
    }

    @GetMapping("userInfo")
    public PageResult login(BlogUser blogUser, HttpSession session){
        logger.info("登录");
        BlogUser loginUser = blogUserService.findByUserName(blogUser.getUsername());
        if (loginUser==null){
            return PageResult.none("用户名错误");
        }
        if (!blogUser.getPassword().equals(loginUser.getPassword())){
            return PageResult.none("密码错误");
        }
        session.setAttribute("loginUser",loginUser);
        return PageResult.success("成功",null);
    }

    @GetMapping("checkUserInfo")
    public PageResult checkUserInfo(HttpSession session){
        if(session.getAttribute("loginUser")==null){
            return PageResult.none("未登录");
        }
        return PageResult.success("已登录",session.getAttribute("loginUser"));
    }
    @GetMapping("checkUserId")
    public PageResult checkUserId(String username, HttpSession session){
        logger.info("查询用户名");
        BlogUser blogUser = blogUserService.findByUserName(username);
        if (blogUser!=null){
            return PageResult.none("用户名称重复");
        }
        return PageResult.success("用户名可用",null);
    }

    @PostMapping("blogUser")
    public PageResult addCatuser(@RequestBody BlogUser blogUser){
        logger.info("注册功能");
        blogUserService.insertBlogUser(blogUser);
        return PageResult.success("注册成功",null);
    }
}
